package guowei.guliorder.order.config;

import com.rabbitmq.client.Channel;
import guowei.guliorder.order.entity.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;

/**
 * 进入ioc容器中的queue,exchange,binding都会自动创建
 */
@Configuration
public class MqDLXConfig {

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue dlxQueue(){
        HashMap<String, Object> map = new HashMap<>();

//  设置死信队列的参数信息，消息传到指定dlx并按指定bindingkey并设置队列过期时间30s
        map.put("x-dead-letter-exchange","order-event-exchange");
        map.put("x-dead-letter-routing-key","order.release.order");
        map.put("x-message-ttl",80000);

        Queue queue = new Queue("order.delay.queue", true, false, false, map);
        return queue;
    }

    /**
     * dlx传入到此队列信息
     * @return
     */
    @Bean
    public Queue commonQueue(){
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public Exchange dlxExcange(){
        TopicExchange topicExchange = new TopicExchange("order-event-exchange", true, false);
        return topicExchange;
    }

    /**
     * 绑定死信队列与dlx
     * @return
     */
    @Bean
    public Binding CreateBinding(){
        Binding binding = new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create.order", null);
        return binding;
    }

    /**
     * dlx与普通队列绑定
     * @return
     */
    @Bean
    public Binding releaseBinding(){
        Binding binding = new Binding("order.release.order.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.order", null);
        return binding;
    }

}
